<template>
  <a-drawer
    title="编辑院系专业招生计划"
    :width="windowWidth"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="departmentsEditVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='院系' v-bind="formItemLayout">
          <a-select :allowClear="true" showSearch optionFilterProp="children"   v-decorator="['zsyxm',{ rules: [{ required: true, message: '请选择院系' }] }]" disabled>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
      </a-form-item>
      <a-form-item label='招生年度'
                   v-bind="formItemLayout">
            <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="['zsnj',{ rules: [{ required: true, message: '请选择学年' }] }]" :disabled="!status">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
      </a-form-item>
      <a-table
        ref="table"
        size="default"
        rowKey="key"
        bordered
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        >
        <template slot="plan" slot-scope="text, record">
            <a-input
            style="margin: -5px 0"
            :value="text.plan"
            @change="e => handleChange(record.key,'plan',e.target.value)"
            :disabled="!status"
            />
        </template>
        </a-table>
        <a-form-item label='备注' style="margin-top:20px">
          <a-textarea placeholder="请输入..." v-decorator="['note',{ rules: [{ required: false, message: '请输入备注' }] }]" />
        </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading" v-if="status">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  name: 'DepartmentsEdit',
  props: {
    info: {
      default: () => ({
        hzfs: '',
        zjh: '',
        fzyxwjh: '',
        zyxwjh: ''
      })
    },
    status: {
      default: true
    },
    departmentsEditVisiable: {
      default: false
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      windowWidth:"650",
			winWidth:"650",
      loading: false,
      data: [],
      postParams: {
        zsyxm: '',
        zsnj:'',
        note:''
      },
      // 表头
      columns: [
        {
          title: '',
          dataIndex: 'column'
        },
        {
          title: '计划',
          scopedSlots: { customRender: 'plan' }
        },
        {
          title: '已分配',
          dataIndex: 'distribution'
        }
      ],
      selectMenu: {
        yx: [] // 院系
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  beforeCreate() {
			this.form = this.$form.createForm(this)
		},
  watch: {
    	winWidth(val){
				let that = this
				setTimeout(function(){
					if(val<650){
						that.windowWidth=val;
					}else{
						that.windowWidth="650"
					}
        },400)
        //console.log(val)
			},
    info (val) {
      // console.log(val)
      this.form.resetFields()
      this.postParams = {
        zsyxm: val.zsyxm,
        zsnj:val.zsnj,
        note:val.note
      }
      this.form.getFieldDecorator('zsyxm',{initialValue:val.zsyxm}) ;
      this.form.getFieldDecorator('zsnj',{initialValue:val.zsnj}) ;
      this.form.getFieldDecorator('note',{initialValue:val.note}) ;
      this.data = [{
        column: '总计划',
        plan: val.zszjhrs,
        distribution: val.zsyfpzrs,
        key: 'zszjhrs'
      }, {
        column: '学术学位计划',
        plan: val.fzyxwjhrs,
        distribution: val.fzyxwyfprs,
        key: 'fzyxwjhrs'
      }, {
        column: '专业学位计划',
        plan: val.zyxwjhrs,
        distribution: val.zyxwyfprs,
        key: 'zyxwjhrs'
      }]
    }
  },
  	mounted(){
      const that = this
      that.windowWidth = document.body.clientWidth>650?650:document.body.clientWidth
			window.onresize=()=>{
				return (()=>{
					window.screenWidth = document.body.clientWidth
					that.winWidth =window.screenWidth
				})()
			}
		},
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    reset () {
      this.data = [{
        column: '总计划',
        plan: '',
        distribution: 1,
        key: 'zjh'
      }, {
        column: '学士学位计划',
        plan: '',
        distribution: 1,
        key: 'fzyxwjh'
      }, {
        column: '专业学位计划',
        plan: '',
        distribution: 1,
        key: 'zyxwjh'
      }]
    },
    // 选择院系
    yxChange (code) {

      this.form.setFieldsValue({'sszym':''})
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
       this.form.validateFields( (err,values) => {
					if(err){
					this.$message.error("请选择必填项")
					}else{
      let paramms = Object.assign({
      	zszjhrs: this.data[0].plan,
      	fzyxwjhrs: this.data[1].plan,
      	zyxwjhrs: this.data[2].plan,
				id:this.info.id,

      },this.form.getFieldsValue())
      // console.log(paramms)
      this.$api.doctorRecruit.collegeEnUpdate(paramms).then(res => {
      	// this.$message.success(res.msg)
      	this.$emit('success')
      }).finally(() => {
      	this.loadingBtn = false
      	// this.form.resetFields()
      })
          }
       })
    },
    // 修改各计划人数人数
    handleChange (key, dataIndex, value) {
      const dataSource = [...this.data]
      const target = dataSource.find(item => item.key === key)
      if (target) {
        target[dataIndex] = value
        this.data = dataSource
      }
    }
  }
}
</script>
